<?php
#by reemo 2012
class ShowDepotPage extends AbstractPage
{
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	function send()
	{
		global $USER, $PLANET, $CONF;
		
		if($CONF['resource_multiplier'] > 500)
				{
					$time_break = TIMESTAMP + 150;
				}
				elseif($CONF['resource_multiplier'] > 50)
					{
						$time_break = TIMESTAMP + 300;
					}
					else
						{
							$time_break = TIMESTAMP + 600;
						}
		
		$ataks	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 1 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		$unic	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 9 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		
		if($ataks !=0 || $unic !=0)
			$this->redirectTo('game.php');
		
			$r901	= max(0, min(floor($PLANET['metal']/10000000), HTTP::_GP('resource901', 0)));
			$r902	= max(0, min(floor($PLANET['crystal']/10000000), HTTP::_GP('resource902', 0)));
			$r903	= max(0, min(floor($PLANET['deuterium']/1000000), HTTP::_GP('resource903', 0)));

			$Cost = 20 * ($r901 + $r902 + $r903);
		
			if($USER['darkmatter'] < $Cost )
			{
				$this->printMessage("Недостаточно Темной материи", true, array("game.php?page=depot", 2));
				return;
			}

			$USER['darkmatter']			-= $Cost;
			$PLANET['metal'] 			-= $r901*1000000;
			$PLANET['crystal'] 			-= $r902*1000000;
			$PLANET['deuterium'] 		-= $r903*1000000;
						
			$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET
				 	`bankm` = `bankm` + ".$r901.",
					`bankc` = `bankc` + ".$r902.",
					`bankd` = `bankd` + ".$r903."
				 	WHERE `id` = '".$USER["id"]."';");			
							
			$this->printMessage("Вклад выполнен", true, array("game.php?page=depot", 2));

	}
	
	public function sendup()
	{
		global $USER, $PLANET, $CONF;
		
		if($CONF['resource_multiplier'] > 500)
				{
					$time_break = TIMESTAMP + 150;
				}
				elseif($CONF['resource_multiplier'] > 50)
					{
						$time_break = TIMESTAMP + 300;
					}
					else
						{
							$time_break = TIMESTAMP + 600;
						}
						
			$ataks	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 1 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
			$unic	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 9 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		
		if($ataks !=0 || $unic !=0)
			$this->redirectTo('game.php');
			
			$r901	= max(0, min($USER['bankm'], HTTP::_GP('r901', 0)));
			$r902	= max(0, min($USER['bankc'], HTTP::_GP('r902', 0)));
			$r903	= max(0, min($USER['bankd'], HTTP::_GP('r903', 0)));

			$PLANET['metal'] 			+= $r901*1000000;
			$PLANET['crystal'] 			+= $r902*1000000;
			$PLANET['deuterium'] 		+= $r903*1000000;
						
			$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET
				 	`bankm` = `bankm` - ".$r901.",
					`bankc` = `bankc` - ".$r902.",
					`bankd` = `bankd` - ".$r903."
				 	WHERE `id` = '".$USER["id"]."';");			
							
			$this->printMessage("Ресурсы извлечены", true, array("game.php?page=depot", 2));


	}
	
	function show()
	{
		global $PLANET, $USER, $CONF;
		
		if($CONF['resource_multiplier'] > 500)
				{
					$time_break = TIMESTAMP + 150;
				}
				elseif($CONF['resource_multiplier'] > 50)
					{
						$time_break = TIMESTAMP + 300;
					}
					else
						{
							$time_break = TIMESTAMP + 600;
						}
		
		$ataks	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 1 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		$unic	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 9 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		
		if( $ataks > 0 || $unic > 0)
		{
			$this->printMessage("Вблизи Вашей планеты замечен вражеский флот... Связь с торговцем недоступна", true, array("game.php?page=", 2));
		}
		
		$this->tplObj->loadscript("trader.js");
		$this->tplObj->assign_vars(array(
			'bankm'				=> $USER['bankm'],
			'bankc'				=> $USER['bankc'],
			'bankd'				=> $USER['bankd'],
		));
		
		$this->display('page.depot.tpl');
	}
}
?>